﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;
using System.Data.SqlClient;


namespace _3tiers
{
    public partial class Form4ThemMon : Form
    {
        public Form4ThemMon()
        {
            InitializeComponent();
        }

        private void Form4ThemMon_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'khoaDataSet.tblKhoa' table. You can move, or remove it, as needed.
            this.tblKhoaTableAdapter.Fill(this.khoaDataSet.tblKhoa);
            // TODO: This line of code loads data into the 'quanLyLichDayDataSet6.tblBoMon' table. You can move, or remove it, as needed.
            //this.tblBoMonTableAdapter.Fill(this.quanLyLichDayDataSet6.tblBoMon);
            try
            {
                //string sql = "select * from tblBoMon";
                string sql = "select TenMon,TenBoMon,SoTinChi from tblBoMon bm,tblMon m where bm.IDBoMon=m.IDBoMon";
                dataGridView1.DataSource = business.ShowData(sql);
                dataGridView1.Show();
            }
            catch { }

            
        }

        private void cbBoMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string IDBoMon = cbBoMon.SelectedValue.ToString();
                string sql = "select TenMon,TenBoMon,SoTinChi from tblBoMon bm,tblMon m where bm.IDBoMon=m.IDBoMon and m.IDBomon='" + IDBoMon + "'";
                dataGridView1.DataSource = business.ShowData(sql);
            }
            catch
            {

            }
        }

        private void btreset_Click(object sender, EventArgs e)
        {
            tbTinChi.Text = "";
            tbTenMon.Text = "";
        }
        DataAccess ac = new DataAccess();
        BLL.BLL business = new BLL.BLL();
        
        private void btThem_Click(object sender, EventArgs e)
        {
            try
            {                
                SqlConnection con = ac.GetConnect();
                con.Open();
                SqlCommand cmd = new SqlCommand("stThemMon", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@idbomon", SqlDbType.NChar).Value = cbBoMon.SelectedValue.ToString();
                cmd.Parameters.Add("@tenmon", SqlDbType.NVarChar).Value = tbTenMon.Text;
                cmd.Parameters.Add("sotinchi", SqlDbType.TinyInt).Value = int.Parse(tbTinChi.Text);
                cmd.ExecuteNonQuery();
                lbThongBao.Text = "Thêm môn thành công.";
            }
            catch { lbThongBao.Text = "Thêm môn thất bại"; }
            Form4ThemMon_Load(sender, e);
            try
            {
                //string sql = "select * from tblBoMon";
                //string sql = "select TenMon,TenBoMon,SoTinChi from tblBoMon bm,tblMon m where bm.IDBoMon=m.IDBoMon";
                string sql = "stSelectAllMon";
                dataGridView1.DataSource = business.ShowData(sql);
                dataGridView1.Show();                
            }
            catch { } 
        }

        private void cbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string IDKhoa = cbKhoa.SelectedValue.ToString();
                string sql = "select * from tblBoMon where IDKhoa='" + IDKhoa + "'";
                DataTable dt = ac.GetTable(sql);
                cbBoMon.DataSource = dt;
                cbBoMon.DisplayMember = "TenBoMon";
                cbBoMon.ValueMember = "IDBoMon";
            }
            catch { };
            try
            {
                string IDKhoa = cbKhoa.SelectedValue.ToString();
                string sql = "select TenMon,TenBoMon,SoTinChi from tblBoMon bm,tblMon m where bm.IDBoMon=m.IDBoMon and IDKhoa='"+IDKhoa+"'";
                dataGridView1.DataSource = business.ShowData(sql);
            }
            catch 
            {
                
            }
        }
    }
}
